/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.codegen.jfinal.writer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.alibaba.fastjson.util.IOUtils;
import com.google.common.base.Charsets;
import com.hyts.codegen.common.writer.JavaCodeWriter;
import com.hyts.codegen.common.abs.AbstractFileWriter;
import com.hyts.codegen.jfinal.config.JFinalWriterConfig;
import com.hyts.guava.exception.Exceptions;
import com.hyts.guava.object.Objects;
import com.hyts.jlogger.factory.LoggerFactory;
import com.hyts.template.common.BaseJavaFileTemplate;
import com.hyts.template.strformat.StringCodeTemplate;

/** 
 * @title FileWriterHandler.java  
 * @package com.hyts.codegen.jfinal.common  
 * @description TODO  
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月10日 
 * @version V1.0 
 */
public final class JfinalCodeWriter implements AbstractFileWriter<String>{

	/**  
	 * <p>日志输出器}</p> 
	 * @fieldName logger
	 * @fieldType Logger
	 */ 
	private static Logger logger = LoggerFactory.getLogger(JavaCodeWriter.class);
	
		
	/* (非 Javadoc)
	 * @param classpath
	 * @param object
	 * @return
	 * @see com.hyts.codegen.generator.AbstractFileWriter#writerInternal(java.lang.String, java.lang.Object)  
	 * @exception
	 */ 
	
	@Override
	public String writerInternal(String classpath, String object) {
		Objects.checkNull(classpath);Objects.checkNull(object);
		String url = JFinalWriterConfig.JFINAL_MAVEN_BASE_PATH
				+ classpath.replace("/",File.separator);
		Path filePath = Paths.get(url);
		logger.info("【coffice-toolbox-codegen】- 生成文件路径文件:"+url);
		File parentFile = filePath.getParent().toFile();
		if(!filePath.getParent().toFile().exists()){
			parentFile.mkdirs();
		}
		BufferedWriter writer = null;
		try{
			Files.deleteIfExists(filePath);
			Files.createFile(filePath);
			writer = Files.newBufferedWriter(filePath, Charsets.UTF_8, LinkOption.NOFOLLOW_LINKS);	
			writer.write(object);
		} catch (IOException e) {
			Exceptions.throwRuntimeException(e);
		} finally{
			IOUtils.close(writer);
		}
		return filePath.toString();
	}

	/* (非 Javadoc)
	 * @param outputFile
	 * @param object
	 * @return
	 * @see com.hyts.codegen.generator.AbstractFileWriter#writerExternal(java.lang.String, java.lang.Object)  
	 * @exception
	 */ 
	
	@Override
	public String writerExternal(String outputFile, String object) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static void main(String[] args) {
		BaseJavaFileTemplate tempate = new StringCodeTemplate();
		tempate.genPackageContent("com.hyts.codegen", new String[]{"test"});
		tempate.genImportContent(new String[]{"import java.util.Map;","import java.util.logging.Logger;"});
		Map<String,String> annotationMap = new HashMap<>();
		annotationMap.put("author","alex/libo");
		annotationMap.put("date","20180509");
		annotationMap.put("description","om.hyts.codegen.generator.AbstractFileWriter#writerEx");
		tempate.genJavaDocContent(annotationMap,"");
		tempate.genTypeContent(null, null, "test", null);
		tempate.genFieldContent(null, "String", "name", "    ", null);
		JavaCodeWriter.builder().build().writerInternal("com/hyts/codegen/test/test.java",tempate.end().toString());
	}

}
